module RuSSY
  class PagePartBase

    @pagepartid
    @testconfig
    @envdata

    def initialize(partid, testconfig, envdata)
      @pagepartid = partid
      @testconfig = testconfig
      @envdata = envdata
    end

    def verify_status(statusname)
      statusdata = @testconfig.getstatusdata(@pagepartid + "." + statusname)
      statusdata.each do |elementname, elementstatus|
        htmlelement = @testconfig.find_element(@pagepartid + "." + elementname)
        elementstatus.each do |statusname, statusvalue|
          case statusname
            when "text"
              htmlelement.text.should == evaluate_text(statusvalue)
              puts "text real value: " + htmlelement.text
              puts "text expect value: " + evaluate_text(statusvalue)
            when "status"
            when "defaultavlue"
            when "content"
            when "style"
          end
        end
      end
    end

    def execute_actions(actiondata)
    end

    def evaluate_envdata(inputdata)
      variablename = inputdata[/\{[a-z0-9_]+\}/]
      if variablename.nil? || @envdata.nil?
        inputdata
      else
        keyname = variablename[1, variablename.length-2]
        if @envdata[keyname].nil?
          inputdata
        else
          inputdata.sub(variablename, @envdata[keyname])
        end
      end
    end

    def evaluate_text(inputdata)
      variablename = inputdata[/\%[a-z0-9_]+\%/]
      if variablename.nil? 
        strtext = inputdata
      else
        keyname = @pagepartid + "." + variablename[1, variablename.length-2]
        strtext = @testconfig.gettext(keyname)
        if strtext.nil?
          strtext = inputdata
        end
      end
      evaluate_envdata(strtext)
    end

    private :evaluate_text, :evaluate_envdata
  end
end
